import Vue from 'vue'
import Router from 'vue-router'
import index from '@/pages/index/index.vue'
import register from '@/pages/register/register.vue'
import Case from '@/pages/case/index.vue'
import store from '../store'
import Recode from '@/pages/recode/index'
import Mistake from '@/pages/mistake/index'
import Select from '@/pages/select/index'
Vue.use(Router)

const routes = [
	{
		path: '/',
		redirect: '/select', // 路由重定向
	},
	{
		path: '/select',
		name: 'select',
		component: Select,
		meta: {
			title: '选择车型',
		}
	},
	{
		path: '/index',
		name: "nav",
		component: () => import('@/components/menu/nav'),
		children: [{
			path: '/index',
			name: 'index',
			component: index,
			meta: {
				title: '首页',
				keepAlive:true
			}
		}]
	},

	{
		path: '/case',
		name: 'case',
		component: Case,

	},
	{
		path: '/recode',
		name: 'recode',
		component: Recode,

	},
	{
		path: '/mistake',
		name: 'mistake',
		component: Mistake,

	},
	{
		path: '/register',
		name: 'register',
		component: register,
		meta: {
			noAuth: true
		}
	},

]



const router = new Router({
	mode: 'history',
	routes
})


router.beforeEach((to, from, next) => {
	if (to.meta.noAuth) return next()
	if (!store.state.userInfo) return next('/register')
	next()

})

export default router
